﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Runtime.Messages;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime.Configuration;

namespace PServiceBus.Core.Manager {
    /// <summary>
    /// Manage addition of failed messages
    /// </summary>
    public static class FailedMessageManager {
        private static IObjectManager<FailedMessage> _messages;

        private static IObjectManager<FailedMessage> Messages {
            get {
                if (_messages == null)
                    _messages = ObjectManager<FailedMessage>.Create(ConfigurationProvider.ServiceBusConfig.DeadMessageAddress);
                return _messages;
            }
        }

        /// <summary>
        /// Attach Message only if it is considered valid
        /// </summary>
        /// <param name="message">The message</param>
        public static void Attach(FailedMessage message) {
            if (message.IsValid) Messages.Attach(message);
        }

        /// <summary>
        /// Attach Messages only if it is considered valid
        /// </summary>
        /// <param name="messages"></param>
        public static void AttachMany(IEnumerable<FailedMessage> messages) {
            Messages.AttachMany(messages.Where(msg => msg.IsValid));
        }
    }
}
